#include <stdio.h>

#define N 15

int main() {
    int n, i, j, k, row, col;
    int magic[N + 1][N + 1] = {0};

    printf("请输入一个奇数n（n<=15）：");
    scanf("%d", &n);

    if (n % 2 == 0) {
        printf("n必须为奇数！\n");
        return 0;
    }

    // 初始化第一行中间位置为1
    row = 1;
    col = n / 2 + 1;
    magic[row][col] = 1;

    // 逐个填充魔方阵
    for (k = 2; k <= n * n; k++) {
        if (row == 1 && col != n) {
            row = n;
            col++;
        } else if (row != 1 && col == n) {
            row--;
            col = 1;
        } else if (row == 1 && col == n) {
            row++;
        } else if (magic[row - 1][col + 1] == 0) {
            row--;
            col++;
        } else {
            row++;
        }

        magic[row][col] = k;
    }

    // 输出魔方阵
    printf("魔方阵如下：\n");
    for (i = 1; i <= n; i++) {
        for (j = 1; j <= n; j++) {
            printf("%d\t", magic[i][j]);
        }
        printf("\n");
    }

    return 0;
}